﻿/*
 * Last review by   : Yaser
 * Last review date : JUL-25-09 
 */
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using ERP.Framework.Components.SerializationHelper.Interfaces;
using ERP.Framework.Components.Utilities.TracingHelper;

namespace ERP.Framework.Components.SerializationHelper
{
    /// <summary>
    /// Binary serializing
    /// </summary>
    public abstract class BinarySerializingManager : ISerializingManager
    {
        #region Members

        private const string TRACING_CATEGORY = "ERP.Framework.Components.SerializationHelper.BinarySerializingManager";

        #endregion

        #region Methods

        /// <summary>
        /// Serialize object
        /// </summary>
        /// <param name="obj">object to serialize</param>
        /// <returns>serialized object as string</returns>
        public static string SerializeObject(object obj)
        {
            TraceUtil.Write(TRACING_CATEGORY, "SerializeObject : Start");
            try
            {
                String serializedString = null;
                MemoryStream memoryStream = new MemoryStream();
                BinaryFormatter xs = new BinaryFormatter();
                xs.Serialize(memoryStream, obj);
                serializedString = Convert.ToBase64String(memoryStream.ToArray());
                TraceUtil.Write(TRACING_CATEGORY, "SerializeObject : End");
                return serializedString;
            }
            catch (Exception e)
            {
                ERP.Framework.Components.ExceptionHandler.ExceptionPublisher.PublishException(e);
                TraceUtil.Write(TRACING_CATEGORY, "SerializeObject : End");
                return null;
            }
        }

        /// <summary>
        /// Deserialize string to object
        /// </summary>
        /// <typeparam name="T">object type</typeparam>
        /// <param name="serializedText">serialization string</param>
        /// <returns>object</returns>
        public static object DeserializeObject<T>(string serializedText)
        {
            TraceUtil.Write(TRACING_CATEGORY, "DeserializeObject : Start");
            try
            {
                MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(serializedText));
                BinaryFormatter xmlTextWriter = new BinaryFormatter();
                TraceUtil.Write(TRACING_CATEGORY, "DeserializeObject : End");
                return (T)xmlTextWriter.Deserialize(memoryStream);
            }
            catch (Exception e)
            {
                ERP.Framework.Components.ExceptionHandler.ExceptionPublisher.PublishException(e);
                TraceUtil.Write(TRACING_CATEGORY, "DeserializeObject : End");
                return null;
            }
        }

        /// <summary>
        /// Serialize object
        /// </summary>
        /// <param name="obj">object to serialize</param>
        /// <returns>serialized object as string</returns>
        string ISerializingManager.SerializeObject(object obj)
        {
            return SerializeObject(obj);
        }

        /// <summary>
        /// Deserialize string to object
        /// </summary>
        /// <typeparam name="T">object type</typeparam>
        /// <param name="serializedText">serialization string</param>
        /// <returns>object</returns>
        object ISerializingManager.DeserializeObject<T>(string serializedText)
        {
            return DeserializeObject<T>(serializedText);
        }

        #endregion
    }
}
